package cc.vv.baselibrary.util

import android.content.Context
import android.os.Build
import android.support.annotation.RequiresApi
import android.util.DisplayMetrics
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText

/**
 * Date 2018/7/12
 * Describe  关于设备信息扩展函数
 *
 * @auther liuxin
 */

/**
 * 获取手机屏幕尺寸_宽
 */
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
fun Context.screenWidth(): Int {
    val wm: WindowManager = this.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    val dm = DisplayMetrics()
    wm.defaultDisplay.getRealMetrics(dm)
    return dm.widthPixels
}

/**
 * 获取手机屏幕尺寸_高 包含虚拟键在内的屏幕高度
 */
@RequiresApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
fun Context.screenHeightWithoutVirtualBar(): Int {
    val wm: WindowManager = this.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    val dm = DisplayMetrics()
    wm.defaultDisplay.getRealMetrics(dm)
    return dm.heightPixels
}

/**
 * 获取手机屏幕尺寸_高 不包含虚拟键在内的屏幕高度
 */
fun Context.screenHeight(): Int {
    val wm: WindowManager = this.getSystemService(Context.WINDOW_SERVICE) as WindowManager
    val dm = DisplayMetrics()
    wm.defaultDisplay.getMetrics(dm)
    return dm.heightPixels
}

/**
 * 弹出软键盘
 *
 * @param context 上下文
 */
fun EditText.showKeyboard(context: Context?) {
    val imm: InputMethodManager = context!!.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.showSoftInput(this, InputMethodManager.RESULT_SHOWN)
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
}

/**
 * 隐藏软键盘
 *
 * @param context 上下文
 */
fun EditText.hideKeyboard(context: Context?) {
    val imm: InputMethodManager = context!!.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(this.windowToken, 0)
}








